package fr.demos.cinepantin.controleur;

import java.io.IOException;
import java.util.ArrayList;

import javax.inject.Inject;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fr.demos.cinepantin.Article;
import fr.demos.cinepantin.Client;
import fr.demos.cinepantin.dao.DaoAdmin;
import fr.demos.cinepantin.dao.DaoClient;
import fr.demos.cinepantin.dao.interfac.AdminDao;
import fr.demos.cinepantin.dao.interfac.ClientDao;
import fr.demos.cinepantin.enumerate.Civilite;
import fr.demos.cinepantin.enumerate.Tva;
import fr.demos.cinepantin.exception.DaoExceptions;

/**
 * @author CQP-DNT Servlet implementation class Inscription
 */
@WebServlet("/Inscription")
public class Inscription extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Inject
	@DaoClient
	private ClientDao clientdao;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Inscription() {
		super();

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher rd;
		// request.setAttribute("page","/inscription.jsp");
		rd = request.getRequestDispatcher("/inscription.jsp");
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Integer idClient = null;
		String email = null;
		String password = null;
		Civilite civilite = null;
		String nom = null;
		String prenom = null;
		String numPortable = null;
		String ajouterClient = email;

		// creer la liste des erreurs
		ArrayList<String> erreurs = new ArrayList<>();

		// HttpSession session = request.getSession();
		RequestDispatcher rd = null;

		// saisir les details d'un client
		// adresse a ajouter

		try {
			idClient = Integer.parseInt(request.getParameter("idClient"));
		} catch (NumberFormatException e) {
			
		}

		try {
			email = (String) request.getParameter("email");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("erreurPage", "Email incorrecte");
		}
		try {
			password = (String) request.getParameter("password");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("erreurPage", "password invalide !");
		}

		try {
			civilite = Civilite.valueOf(request.getParameter("civilite"));
		} catch (NullPointerException e) {
			erreurs.add("choissisez votre civilité");
		}

		nom = (String) request.getParameter("nom");
		if (nom == null || nom.equals("")) {
			erreurs.add("Veuillez rentrer votre Nom");
		}
		if (nom != null && nom.length() > 30) {
			erreurs.add("Le nom depasse la taille du champs");
		}

		prenom = (String) request.getParameter("prenom");
		if (prenom == null || prenom.equals("")) {
			request.setAttribute("erreurPage", "Saisie prenom incorrecte");
		}

		try {
			numPortable = (String) request.getParameter("numPortable");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("erreurPage", "Saisez votre numero portable");
		}

		if (erreurs.size() > 0) {
			request.setAttribute("erreurs", erreurs);
			request.setAttribute("email", email);
			request.setAttribute("password", password);
			request.setAttribute("civilite", civilite);
			request.setAttribute("nom", nom);
			request.setAttribute("prenom", prenom);
			request.setAttribute("numPortable", numPortable);

			request.setAttribute("Création non effectuée dans la base ",
					erreurs);
			request.setAttribute("page", "/inscription.jsp");
		} else {

			Client cli = null;

			cli = new Client(idClient, email, password, civilite, nom, prenom,
					numPortable);

			try {
				clientdao.enregistrerClient(cli);
				request.setAttribute(
						"Merci pour l'enregistrement : Nom Client = " + nom
								+ "dans le base", ajouterClient);
				request.getRequestDispatcher("/AfficherPanier").forward(
						request, response);
			} catch (DaoExceptions e) {
				e.printStackTrace();
				erreurs.add("cet email existe deja");
				request.setAttribute("page", "/inscription.jsp");
			}

		}
		rd = request.getRequestDispatcher("/AfficherPanier");
		rd.forward(request, response);

	}
}
